from django.http import HttpResponse
from django.shortcuts import render_to_response;
import socket

PORT = 1999 # The same port as used by the server

# GUI for iPad
def gui(request):
  return render_to_response('gui.html');

def pose(request):
  HOST = 'localhost'  # The remote host
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((HOST, PORT))
  s.send('pose?')
  data = s.recv(1024)
  s.close()
  pose = data.split()
  px = float(pose[0]) 
  py = float(pose[1])
  pa = float(pose[2])
  print 'Received', repr((px, py, pa))
  xml_pose = \
  """
  <pose>
    <px>%.2f</px>
    <py>%.2f</py>
    <pa>%.2f</pa>
  </pose>
  """ % (px, py, pa)
  return HttpResponse(xml_pose, mimetype='application/xml');

def set_mission(request):
  HOST = 'localhost'  # The remote host
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((HOST, PORT))
  px = request.POST['px']
  py = request.POST['py']
  pa = request.POST['pa']
  s.send('%s %s %s\n' % (px, py, pa))
  data = s.recv(1024)
  s.close()
  print 'Received', repr(data)
  return HttpResponse(data);
